#!/bin/bash
# 开启各个 worker 节点的任务监听；本脚本在master节点上以 postgres 用户执行

cd $(dirname $0)

# 获得 worker 节点清单
psql_conn_master="psql -U postgres -c"
select_node_sql_worker="select node_id, node_ipv4, node_port, fdw_username, fdw_password, node_status from sm_rdd.tb_snap_cluster_node_list where is_worker is true and node_status = '1'"
node_list_worker=$($psql_conn_master "${select_node_sql_worker}")

# 遍历 worker 节点清单，执行启停动作
line_no=1
for node_record in $node_list_worker
do
  if [ $line_no -gt 1 ]
  then
    node_id=`echo $node_record | perl -F"," -alne '{print @F[0] if $.==1}'`;node_id=`echo $node_id | awk '{printf("%12d\n",$0)}'`
    node_ipv4=`echo $node_record | perl -F"," -alne '{print @F[1] if $.==1}'`
    node_port=`echo $node_record | perl -F"," -alne '{print @F[2] if $.==1}'`
    fdw_username=`echo $node_record | perl -F"," -alne '{print @F[3] if $.==1}'`
    fdw_password=`echo $node_record | perl -F"," -alne '{print @F[4] if $.==1}'`
    
    nohup psql -h $node_ipv4 -U $fdw_username -p $node_port -W$fdw_password -c "call sm_rdd.prc_rdd_work()" &
    
    let line_no=$line_no+1
  fi
done